﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DoubleH.Utility;
using Table = FCNS.Data.Table;

namespace ReportS
{
    /// <summary>
    /// PfTongJi.xaml 的交互逻辑
    /// </summary>
    public partial class ShTongJi : Window
    {
        public ShTongJi()
        {
            InitializeComponent();

            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
                return;

            InitVar();
            InitEvent();
        }

        DataGridTextColumn gcUser = new DataGridTextColumn();
        DataGridTextColumn gcCorS = new DataGridTextColumn();
        List<Table.UniqueS> uniqueList = Table.UniqueS.GetList(Table.UniqueS.EnumFlag.服务评价, Table.UniqueS.EnumEnable.启用);

        public void InitVar()
        {
            uCSearchOrder1.Init(DataTableText.售后统计表);

            dataGridExt1.FormatLastRow = true;

            gcUser.Header = "工程师";
            gcUser.Binding = new Binding("[User]");
            dataGridExt1.Columns.Add(gcUser);
            gcCorS.Header = "客商";
            gcCorS.Binding = new Binding("[CorS]");
            dataGridExt1.Columns.Add(gcCorS);
            DataGridTextColumn gc3 = new DataGridTextColumn();
            gc3.Header = "单据数量";
            gc3.Binding = new Binding("[Quantity]");
            dataGridExt1.Columns.Add(gc3);
            DataGridTextColumn gc4 = new DataGridTextColumn();
            gc4.Header = "作废单据";
            gc4.Binding = new Binding("[QuantityZuoFei]");
            dataGridExt1.Columns.Add(gc4);
            DataGridTextColumn gc51 = new DataGridTextColumn();
            gc51.Header = "免费单据";
            gc51.Binding = new Binding("[QuantityNoMoney]");
            dataGridExt1.Columns.Add(gc51);
            DataGridTextColumn gc5 = new DataGridTextColumn();
            gc5.Header = "收款单据";
            gc5.Binding = new Binding("[QuantityHaveMoney]");
            dataGridExt1.Columns.Add(gc5);
            DataGridTextColumn gc6 = new DataGridTextColumn();
            gc6.Header = "金额";
            gc6.Binding = new Binding("[Money]");
            dataGridExt1.Columns.Add(gc6);
            DataGridTextColumn gc7 = new DataGridTextColumn();
            gc7.Header = "回访收款不符";
            gc7.Binding = new Binding("[MoneyError]");
            dataGridExt1.Columns.Add(gc7);

            foreach (Table.UniqueS us in uniqueList)
            {
                DataGridTextColumn gc = new DataGridTextColumn();
                gc.Header = us.Name;
                gc.Binding = new Binding("["+us.Name+"]");
                dataGridExt1.Columns.Add(gc);
            }
        }

        private void InitEvent()
        {
            buttonSearch.Click += (ss, ee) => {
                this.Cursor = Cursors.Wait;
                FillData();
                this.Cursor = Cursors.Arrow;
            };            
        }

        bool? byUser = null;
        private void FillData()
        {
            if (radioButtonCorS.IsChecked.Value)
            {
                if (byUser != true)
                {
                    byUser = true;
                    gcUser.Visibility = Visibility.Collapsed;
                    gcCorS.Visibility = Visibility.Visible;
                }

                dataGridExt1.ItemsSource = FilterByCorS();
            }
            else
            {
                if (byUser != false)
                {
                    byUser = false;
                    gcUser.Visibility = Visibility.Visible;
                    gcCorS.Visibility = Visibility.Collapsed;
                }
                dataGridExt1.ItemsSource = FilterByUser();
            }
        }

        private List<Dictionary<string, object>> FilterByUser()
        {
           List<Dictionary<string, object>> returnObj = new List<Dictionary<string, object>>();
            List<Table.AfterSaleServiceS> obj = (List<Table.AfterSaleServiceS>)uCSearchOrder1.GetSearchData();
            if (obj == null)
                return returnObj;

            foreach (Table.UserS user in Table.UserS.GetList(Table.UserS.EnumEnable.启用, Table.UserS.EnumFlag.经办人))
            {
                var vr = from f in obj where f.ServiceUserS.Split(';').Contains(user.Id.ToString()) select f;
                if (vr.Count() == 0)
                    continue;

                Dictionary<string, object> ot = NewDic();
                ot["User"] = user.Name;
                foreach (Table.AfterSaleServiceS ass in vr)
                {
                    ot["Quantity"] = (int)ot["Quantity"] + 1;
                    if (ass.Enable == Table.AfterSaleServiceS.EnumEnable.作废)
                    {
                        ot["QuantityZuoFei"] = (int)ot["QuantityZuoFei"] + 1;
                        continue;
                    }
                    double money = ass.FuWuFei + ass.JiaoTongFei;
                    if (money > 0)
                    {
                        ot["Money"] = (double)ot["Money"] + money;
                        ot["QuantityHaveMoney"] = (int)ot["QuantityHaveMoney"] + 1;
                    }
                    else
                        ot["QuantityNoMoney"] = (int)ot["QuantityNoMoney"] + 1;

                    if (money != ass.FuWuFeiHeShi)
                        ot["MoneyError"] = (int)ot["MoneyError"] + 1;

                    Table.UniqueS us = uniqueList.Find(f => { return f.Id == ass.FuWuPingJia; });
                    if (us != null)
                        ot[us.Name] =(int)ot[us.Name]+ 1;
                }
                returnObj.Add(ot);
            }

            Sum(returnObj);
            return returnObj;
        }

        private List<Dictionary<string, object>> FilterByCorS()
        {
            List<Dictionary<string, object>> returnObj = new List<Dictionary<string, object>>();
            List<Table.AfterSaleServiceS> obj = (List<Table.AfterSaleServiceS>)uCSearchOrder1.GetSearchData();
            if (obj == null)
                return returnObj;

            Dictionary<Int64, string> cors = new Dictionary<long, string>();
            obj.ForEach(f => { if (!cors.Keys.Contains(f.CorSId))cors.Add(f.CorSId, f._CorSName); });

            foreach (Int64 id in cors.Keys)
            {
                var vr = from f in obj where f.CorSId == id select f;
                if (vr.Count() == 0)
                    continue;

                Dictionary<string, object> ot = NewDic();

                ot["CorS"] = cors[id];
                foreach (Table.AfterSaleServiceS ass in vr)
                {
                    ot["Quantity"] = (int)ot["Quantity"] + 1;
                    if (ass.Enable == Table.AfterSaleServiceS.EnumEnable.作废)
                    {
                        ot["QuantityZuoFei"] = (int)ot["QuantityZuoFei"] + 1;
                        continue;
                    }
                    double money = ass.FuWuFei + ass.JiaoTongFei;
                    if (money > 0)
                    {
                        ot["Money"] = (double)ot["Money"] + money;
                        ot["QuantityHaveMoney"] = (int)ot["QuantityHaveMoney"] + 1;
                    }
                    else
                        ot["QuantityNoMoney"] = (int)ot["QuantityNoMoney"] + 1;

                    if (money != ass.FuWuFeiHeShi)
                        ot["MoneyError"] = (int)ot["MoneyError"] + 1;

                    ot[ass._FuWuPingJiaName] = (int)ot[ass._FuWuPingJiaName] + 1;
                }

                returnObj.Add(ot);
            }

            Sum(returnObj);
            return returnObj;
        }

        private void Sum(List<Dictionary<string, object>> returnObj)
        {
            if (returnObj.Count == 0)
                return;

            Dictionary<string, object> ot = new Dictionary<string, object>() ;
            ot["CorS"] = "合计";
            ot["User"] = "合计";
            ot["Quantity"] = returnObj.Sum(f => { return (int)f["Quantity"]; });
            ot["Money"] = returnObj.Sum(f => { return (double)f["Money"]; });
            ot["QuantityHaveMoney"] = returnObj.Sum(f => { return (int)f["QuantityHaveMoney"]; });
            ot["QuantityZuoFei"] = returnObj.Sum(f => { return (int)f["QuantityZuoFei"]; });
            ot["MoneyError"] = returnObj.Sum(f => { return (int)f["MoneyError"]; });
            returnObj.Add(ot);
        }

        private Dictionary<string, object> NewDic()
        {
            Dictionary<string, object> ot = new Dictionary<string, object>();
            ot.Add("User", "");
            ot.Add("Quantity", 0);
            ot.Add("QuantityZuoFei", 0);
            ot.Add("Money", 0d);
            ot.Add("QuantityHaveMoney", 0);
            ot.Add("QuantityNoMoney", 0);
            ot.Add("MoneyError", 0);
            uniqueList.ForEach(f => { ot.Add(f.Name, 0); });

            return ot;
        }
    }
}